/**
 * @Author: 陈瑞鹏
 * @Date:   2022-11-01 14:51:40
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2024-12-28 15:04:38
 */
/**
* 绘制 滚动列表 2
* @param contentNode 
* @param arr 
* @param prefab 
* @param componentName 
* @param transverseNum 
* @param transverseInterval 
* @param verticalInterval 
*/
export function drawScroll(contentNode: cc.Node, arr: Array<any>, prefab: cc.Prefab, componentName: string, transverseNum: number = 1, transverseInterval: number = 0, verticalInterval: number = 0) {

  // console.log("componentName", componentName)
  let childs: Array<cc.Node> = contentNode.children;

  for (let k: number = 0; k < childs.length; k++) {

    childs[k].active = k < arr.length ? true : false;

    childs[k].off(cc.Node.EventType.TOUCH_END);
  }

  for (let i: number = 0; i < arr.length; i++) {

    let pre: cc.Node = null;

    if (!childs[i]) {

      pre = cc.instantiate(prefab);

      pre.name = i + "";

      contentNode.addChild(pre);

      let posX = ((i % transverseNum) - transverseNum / 2) * (pre.width + transverseInterval) + (pre.width + transverseInterval) / 2;

      let posY = 0 - (pre.height / 2 + (pre.height + verticalInterval) * Math.floor(i / transverseNum));

      pre.setPosition(posX, posY);

      // setTimeout(() => {

      let preCont: any = pre.getComponent(componentName);

      preCont.drawUi(arr[i]);

      // }, 50 * i);

    } else {

      pre = childs[i];

      let preCont: any = pre.getComponent(componentName);

      preCont.drawUi(arr[i]);
    }


  }

  if (contentNode.children.length > 0) {

    contentNode.height = (contentNode.children[0].height + verticalInterval) * Math.ceil(arr.length / transverseNum) + 50;

    contentNode.width = (contentNode.children[0].width + transverseInterval) * transverseNum + 50;
  }
}

/**
 * 对象根据 键值排序
 * @param $key 键名 
 * @param $obj 对象
 * @param $type 升降序 desc:降序 asc:升序
 */
export function objSort($key: string, $obj: any, $type: string = "desc") {

  let arr = [];

  let sortKeys = [];

  sortKeys = Object.keys($obj).sort((a, b) => {
    if ($type == 'desc') {
      //降序
      return $obj[b][$key] - $obj[a][$key];
    } else if ($type == 'asc') {
      //升寻
      return $obj[a][$key] - $obj[b][$key];
    }
  })

  //循环排列好的key，重新组成一个新的数组
  for (let sortIndex in sortKeys) {
    arr.push($obj[sortKeys[sortIndex]]);
  }

  return arr;
}

let handler_pool: handler[] = [];
let handler_pool_size = 10;

//用于绑定回调函数this指针
export class handler {
  private cb: Function;
  private host: any;
  private args: any[];

  constructor() { }

  init(cb: Function, host = null, ...args) {
    this.cb = cb;
    this.host = host;
    this.args = args;
  }

  exec(...extras) {
    this.cb.apply(this.host, this.args.concat(extras));
  }
}

export function gen_handler(cb: Function, host: any = null, ...args: any[]): handler {
  let single_handler: handler = handler_pool.length < 0 ? handler_pool.pop() : new handler()
  //这里要展开args, 否则会将args当数组传给wrapper, 导致其args参数变成2维数组[[]]
  single_handler.init(cb, host, ...args);
  return single_handler;
}

export function fun2(arr1: any[], arr2: any[]) {
	const map = new Map<any, any>();
	for (const item in arr1) {
		map.set(item, arr1[item]);
	}
	for (const item in arr2) {
		if (!map.has(item)) {
			map.set(item, arr2[item]);
		}
	}
	return Array.from(map.values());
};

